#include "MYusart.h"
#include "stdio.h"
#include "stdarg.h"

#ifdef UART1
unsigned char Uart1_buffer[Buffer_size] = {0};
extern UART_HandleTypeDef huart1; //声明串口
unsigned char Uart1_Rx_Cnt = 0;
unsigned char aRxBuffer1 = 0;
#endif
#ifdef UART2
unsigned char Uart2_buffer[Buffer_size] = {0};
extern UART_HandleTypeDef huart2; //声明串口
unsigned char Uart2_Rx_Cnt = 0;
unsigned char aRxBuffer2 = 0;
#endif
#ifdef UART3
unsigned char Uart3_buffer[Buffer_size] = {0};
extern UART_HandleTypeDef huart3; //声明串口
unsigned char Uart3_Rx_Cnt = 0;
unsigned char aRxBuffer3 = 0;
#endif
#ifdef UART6
unsigned char Uart6_buffer[Buffer_size] = {0};
extern UART_HandleTypeDef huart6; //声明串口
unsigned char Uart6_Rx_Cnt = 0;
unsigned char aRxBuffer6 = 0;
#endif

unsigned char Printf_buf[256] = {0};

void Uart_Printf(char *fmt, ...)
{
	uint8_t Uart_Printf_Buf[Buffer_size] = {0};
	va_list args;
	va_start(args, fmt);
	vsnprintf((char *)Uart_Printf_Buf, Buffer_size, (char *)fmt, args);
	va_end(args);
	HAL_UART_Transmit(&huart1, Uart_Printf_Buf, Buffer_size, 0xff);
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	/* Prevent unused argument(s) compilation warning */
	UNUSED(huart);
	/* NOTE: This function Should not be modified, when the callback is needed,
           the HAL_UART_TxCpltCallback could be implemented in the user file   */
#ifdef Uart1
	if (huart == &huart1)
	{
		Uart1_buffer[Uart1_Rx_Cnt++] = aRxBuffer1;												  //接收数据转存
		if ((Uart1_buffer[Uart1_Rx_Cnt - 1] == 0x0A) && (Uart1_buffer[Uart1_Rx_Cnt - 2] == 0x0D)) //判断结束位
		{
			Uart1_Rx_Cnt = 0;
		}
		HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer1, 1);
	}
#endif

#ifdef Uart2
	if (huart == &huart2)
	{
		Uart2_buffer[Uart2_Rx_Cnt++] = aRxBuffer2;												  //接收数据转存
		if ((Uart2_buffer[Uart2_Rx_Cnt - 1] == 0x0A) && (Uart2_buffer[Uart2_Rx_Cnt - 2] == 0x0D)) //判断结束位
		{
			Uart2_Rx_Cnt = 0;
		}
		HAL_UART_Receive_IT(&huart2, (uint8_t *)&aRxBuffer2, 1);
	}
#endif

#ifdef Uart3
	if (huart == &huart3)
	{
		Uart3_buffer[Uart3_Rx_Cnt++] = aRxBuffer3;												  //接收数据转存
		if ((Uart3_buffer[Uart3_Rx_Cnt - 1] == 0x0A) && (Uart3_buffer[Uart3_Rx_Cnt - 2] == 0x0D)) //判断结束位
		{
			Uart3_Rx_Cnt = 0;
		}
		HAL_UART_Receive_IT(&huart3, (uint8_t *)&aRxBuffer3, 1); //再开启接收中断
	}
#endif

#ifdef Uart6
	if (huart == &huart6)
	{
		Uart6_buffer[Uart6_Rx_Cnt++] = aRxBuffer6;												  //接收数据转存
		if ((Uart6_buffer[Uart6_Rx_Cnt - 1] == 0x0A) && (Uart6_buffer[Uart6_Rx_Cnt - 2] == 0x0D)) //判断结束位
		{
			Uart6_Rx_Cnt = 0;
		}
		HAL_UART_Receive_IT(&huart6, (uint8_t *)&aRxBuffer6, 1); //再开启接收中断
	}
#endif
}
